package com.fenqing.leetcode.code.t0_200.t0_100;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.HashSet;
import java.util.Set;

public class Topic003 extends Topic {

    /**
     * 构造器
     * @param input 输入
     */
    public Topic003(String input) {
        super(input);
    }

    @Execute
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int max = 0;
        for (int i = 0; i < chars.length; i++) {
            int len = lengthOfLongestSubstring(chars, i);
            if (len > max) {
                max = len;
            }
            if (chars.length - i < max) {
                return max;
            };
        }
        return max;
    }

    public int lengthOfLongestSubstring(char[] s, int start){
        Set<Character> hash = new HashSet<>();
        for (int i = start; i < s.length; i++) {
            char ch = s[i];
            if (hash.contains(ch)) {
                return i - start;
            } else {
                hash.add(ch);
            }
        }
        return s.length - start;
    }

    public static void main(String[] args) {
        Topic003 topic003 = new Topic003(" ");
        Integer run = topic003.run();
        System.out.println(run);
    }
}
